package com.stu.student_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.student_back_end.common.Constants;
import com.stu.student_back_end.common.Result;
import com.stu.student_back_end.controller.dto.AccountDTO;
import com.stu.student_back_end.controller.dto.CountDTO;
import com.stu.student_back_end.controller.dto.LoginDTO;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblAccount;
import com.stu.student_back_end.entity.TblBuilding;
import com.stu.student_back_end.mapper.TblAccountMapper;
import com.stu.student_back_end.service.ITblAccountService;
import com.stu.student_back_end.service.ITblBuildingService;
import com.stu.student_back_end.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TblAccountServiceImpl extends ServiceImpl<TblAccountMapper, TblAccount> implements ITblAccountService {

    @Autowired
    private ITblBuildingService buildingService;

    public Result login(AccountDTO accountDTO) {
            //查询账号
            TblAccount account = getOne(new LambdaQueryWrapper<TblAccount>().eq(TblAccount::getUsername, accountDTO.getUsername()));
            //查询到账号
            if (account != null) {
                //校验密码密码
                if(account.getPassword().equals(accountDTO.getPassword())) {
                    String token = TokenUtil.createToken(account.getId(),account.getPassword());
                    LoginDTO loginDTO = new LoginDTO();
                    loginDTO.setToken(token);
                    loginDTO.setAccount(account);
                    loginDTO.setUserType("宿管");
                    TokenUtil.setToken(token);
                    return Result.success("登录成功",loginDTO);
                }else {
                    return Result.error(Constants.CODE_600,"密码错误");
                }
            }else {
                return Result.error(Constants.CODE_600,"账号错误");
            }
    }

    @Override
    public Boolean addAccount(TblAccount account) {
        boolean save = save(account);
        return save;
    }

    @Override
    public Page<TblAccount> queryAccount(PageDTO pageDTO) {
        Page<TblAccount> page =new Page<>(pageDTO.getCurrentPage(),pageDTO.getSizePage());
        Page<TblAccount> accountPage = new Page<>();
        //判断是否带关键字
        if(!(pageDTO.getKeyWord() == "")) {
            //不为空
            accountPage = page(page, new LambdaQueryWrapper<TblAccount>().like(TblAccount::getUsername, pageDTO.getKeyWord()));
        }else {
            accountPage = page(page);
        }
        return accountPage;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean delAccountAndBuilding(int id,String name) {
        //删除account表的数据
        boolean removeById = removeById(id);

        //删除building的数据
        boolean update = buildingService.update(new LambdaUpdateWrapper<TblBuilding>()
                .eq(TblBuilding::getManager, name)
                .set(TblBuilding::getManager, "无"));
        //判断是否为真
        if(!removeById && !update) throw new RuntimeException();

        return true;
    }

    @Override
    public CountDTO queryAccountCount() {
        CountDTO countVo = new CountDTO();
        countVo.setName("宿管");
        countVo.setCount(baseMapper.queryAccountCount());
        return countVo;
    }
}
